package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品管理")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    /**
     *
     */
    @ApiOperation("新增菜品管理")
    @PostMapping
    public Result save(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品数据：{}", dishDTO);
        dishService.addDishWithFlavors(dishDTO);
        // 清理缓存
        String key = "dish_" + dishDTO.getCategoryId();
        clearRedisCache(key);
        return Result.success();
    }

    /**
     * 菜品分页查询
     */
    @GetMapping("/page")
    @ApiOperation("分页菜品")
    public Result<PageResult> list(DishPageQueryDTO dishPageQueryDTO) {
        PageResult page = dishService.list(dishPageQueryDTO);
        return Result.success(page);
    }

    /**
     * 菜品的批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除")
    public Result del(@RequestParam Long[] ids) {
        log.info("菜品的批量删除：{}", ids);
        dishService.delDishes(ids);

        // 将所有菜品缓存清理
        clearRedisCache("dish_*");
        return Result.success();
    }

    /**
     * 根据ID查询菜品
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据ID查询菜品")
    public Result<DishVO> getById(@PathVariable Long id) {
        log.info("根据ID查询菜品:{}", id);
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品和口味
     *
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品和口味")
    public Result updateDish(@RequestBody DishDTO dishDTO) {
        log.info("修改的菜品数据：{}", dishDTO);
        dishService.updateDishAndFlavor(dishDTO);

        // 需要判断是否修改分类  如果修改了分类  影响了两份缓存数据  修改过于复杂繁琐
        // 将所有菜品缓存清理
        clearRedisCache("dish_*");


        return Result.success();
    }

    /**
     * 修改菜品状态
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("修改菜品状态")
    public Result updateStatus(@PathVariable Integer status, Long id) {
        log.info("修改商品状态：{}", status);
        dishService.editStatus(status, id);

        // 将所有菜品缓存清理  需要取查询数据哪个商品启售停售
        clearRedisCache("dish_*");

        return Result.success();
    }

    /**
     * 根据ID查询菜品
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类ID查询菜品")
    public Result<List<Dish>> listByCategoryId(Long categoryId) {
        List<Dish> dish = dishService.listByCategoryId(categoryId);
        return Result.success(dish);
    }

    private void clearRedisCache(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
